package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.FinancialDaily;
import com.my.bean.ResultPage;

import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.FinancialDailyService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 财务日报 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-10-29
 */
@Controller
@RequestMapping("/financialDaily")
public class FinancialDailyController {
    @Autowired
    FinancialDailyService financialDailyService;
    Logger logger = Logger.getLogger(SituationAnalysisController.class);

    /**
     * 返回财务日报页面
     *
     * @return
     */
    @RequestMapping("/dayMoney1")
    public String financialDaily() {
        return "dayMoney1";
    }


    /**
     * 返回财务日报添加页面
     *
     * @return
     */
    @RequestMapping("/dayMoney1_add")
    public String dayMoney1Add() {
        return "/Alert/dayMoney1_add";
    }

    /**
     * 返回财务日详细信息页面
     */
    @RequestMapping("/alertDayMoney")
    public String dayMoneyEdit(Long createTime, Model model) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(createTime);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        model.addAttribute("res",res);
        //System.out.println("res:"+res);
        return "/Alert/alertDayMoney";
    }


    //查询的是同一天的页面只显示一条数据
    @RequestMapping("/selectdaymoney")
    @ResponseBody
    public ResultPage<FinancialDaily> selectdaymoney(String lin, String data, VoPage voPage, HttpSession session) {
        boolean admin = false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if (SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")) {
            admin = true;
        }
        Map map = new HashMap();
        map.put("lin", lin);
        map.put("data", data);
        //获取谁登陆的就是谁的id
        //User user = SessionUtil.getSessionUser(session);
        //获取到用户的id
        //User user=SessionUtil.getSessionUser(session);
        //user.getId();
        Long areaid = ((User) session.getAttribute("user")).getAreaId();
        ResultPage<FinancialDaily> list = financialDailyService.selectMyPage(map, voPage, session, areaid, admin);
        logger.info("查询成功");
        return list;
    }



    //查询的是根据日期来查询今天一共有多少数据
    @RequestMapping("/selectdaymoney2")
    @ResponseBody
    public ResultPage<FinancialDaily> selectdaymoney2(String lin, String data, VoPage voPage, HttpSession session,String res) {
        //System.out.println("res= "+res);
        boolean admin = false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if (SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")) {
            admin = true;
        }
        Map map = new HashMap();
        map.put("lin", lin);
        map.put("data", data);
        Long areaid = ((User) session.getAttribute("user")).getAreaId();
      ResultPage<FinancialDaily> list = financialDailyService.selectMyPage2(map, voPage, session, areaid, admin,res);
        logger.info("查询成功");
        return list;
    }



    /**
     * t添加财务日报
     * @param param
     * @return
     */
    @RequestMapping("/insertDayMoney1")
    @ResponseBody
    public boolean insertDayMoney1(String param,HttpSession session){
        //把json字符串转换成对应的实体类对象
        FinancialDaily financialDaily = JSONObject.parseObject(param,FinancialDaily.class);
        //获取当前时间
        financialDaily.setCreateTime(new Date().getTime());

        Long areaid=((User)session.getAttribute("user")).getAreaId();
        financialDaily.setAreaid(areaid);
        boolean flag = financialDailyService.insert(financialDaily);
        String msg = flag ? "添加成功" + financialDaily : "添加失败";
        logger.info(msg);
        return flag;
    }



}